package com.itchu.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itchu.common.domain.PageResult;
import com.itchu.system.domain.Category;
import com.itchu.system.mapper.CategoryMapper;
import com.itchu.system.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据分类的父id查找出父级分类
     * @param pid
     * @return
     */
    @Override
    public List<Category> showCategoryByPid(Long pid) {
        Category category = new Category();
        category.setParentId(pid);
        List<Category> list = categoryMapper.select(category);
        list.stream().forEach(c -> {
            category.setParentId(c.getId());
            List<Category> childs = categoryMapper.select(category);
            if (!CollectionUtils.isEmpty(childs)) {
                c.setChild(childs);
            }
        });
        return list;
    }

    /**
     * 根据歌曲id查找出所归属的分类
     * @param id  歌曲id
     * @return
     */
    @Override
    public List<Category> findCategoriesBySid(Long id) {
        List<Category> list = categoryMapper.findCategoriesBySid(id);
        return list;
    }

    /**
     * 根据歌单id查找出分类
     * @param id  歌单id
     * @return
     */
    @Override
    public List<Category> findCategoriesByLid(Long id) {
        List<Category> list = categoryMapper.findCategoriesByLid(id);
        return list;
    }

    /**
     * 查找分类的分页信息
     * @param key   搜索条件
     * @param page  当前页
     * @param rows  页大小
     * @param sortBy
     * @param desc
     * @return
     */
    @Override
    public PageResult<Category> findByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //开启分页
        PageHelper.startPage(page,rows);
        //创建一个封装条件的对象
        Example example = new Example(Category.class);
        if (!StringUtils.isEmpty(key)) {
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("name","%" + key + "%");
            criteria.orLike("id","%" + key + "%");
        }
        if (!StringUtils.isEmpty(sortBy)) {
            example.setOrderByClause(sortBy+" "+(desc ? "desc" : "asc"));
        }
        List<Category> categorys = categoryMapper.selectByExample(example);
        PageInfo<Category> pageInfo = new PageInfo<>(categorys);
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getPages(),categorys);
    }

    /**
     * 添加分类
     * @param category
     * @return
     */
    @Override
    public void addCategory(Category category) {

        if (category != null) {
            category.setCreateTime(new Date());
            category.setUpdateTime(new Date());
            categoryMapper.insertSelective(category);
        }
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @Override
    public void updateCategory(Category category) {
        if (category != null) {
            category.setUpdateTime(new Date());
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @Override
    public void deletCategory(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据分类id查找出分类
     * @param id  分类id
     * @return
     */
    @Override
    public Category findById(Long id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        return category;
    }
}
